Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Introduction to Array in Python
Python - Array Methods
Quiz
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python Slicing Strings

Slicing is a powerful feature in Python that allows you to extract a substring from a string. This is done by specifying the start point and end point of the slice, making it a versatile tool for handling text data.

Slicing can be performed with positive or negative indices, and you can define the slice to include every nth character. Understanding how to slice strings correctly can help in data parsing, manipulation, and cleaning processes, which are common in web development and data science.

Syntax of String Slicing

To slice a string in Python, you use the slice notation, which includes brackets [] containing a start index, an end index, and an optional step:

Python3
. . . .
  • Start index: The beginning position where the slice starts (inclusive).
  • End index: The position where the slice ends (exclusive). If omitted, slices through the end of the string.
  • Step: A value that allows skipping characters. For instance, a step of 2 skips every other character.

Examples Demonstrating String Slicing

Let’s explore slicing with various examples to cover positive and negative indexing, slicing from start to end, and using steps.

Example: Positive Indexing

Positive indexing starts counting from 0 at the beginning of the string.

Python3

. . . .

Explanation:

  • text[7:13]: Extracts characters from index 7 up to, but not including, index 13. This slice includes the substring "Python".

Example: Negative Indexing

Negative indexing starts counting backward from -1 at the end of the string.

Python3

. . . .

Explanation:

  • text[-5:-1]: Extracts characters starting from the fifth-last ('t') up to, but not including, the last character ('!'). This captures the substring "thon".

Example: Slicing from the Start or End

You can omit the start index to begin from the start, or omit the end index to go through to the end of the string.

Python3

. . . .

Explanation:

  • text[:6]: Extracts characters from the start of the string up to, but not including, index 6, resulting in "Hello,".
  • text[7:]: Starts the slice at index 7 and goes to the end of the string, capturing "Python!".

Example: Using Steps in Slicing

The step value in slicing lets you skip characters and extract parts of the string accordingly.

Python3

. . . .

Explanation:

  • text[::2]: Slices the entire string but picks every second character, starting from the first. It selects "H", skips "e", selects "l", and so on, resulting in "Hlo, yhn!".

These examples demonstrate how slicing can be used to efficiently manipulate and extract required parts of strings in Python. By mastering string slicing, you can handle text data more effectively, whether you're developing applications, scripting, or doing data analysis.

Python - String Operators
Python - String Methods
Mark as Completed

On this page

Syntax of String Slicing

Examples Demonstrating String Slicing

Example: Positive Indexing

Example: Negative Indexing

Example: Slicing from the Start or End

Example: Using Steps in Slicing